package com.ruleengine.endpoint.callback.impl;

import org.apache.log4j.Logger;

import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.context.IWorkflowContextConstants;
import com.ruleengine.endpoint.callback.IEndpointCallBack;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ruleengine.frame.impl.WorkflowExecuter;

public class DefaultEndpointCallback implements IEndpointCallBack {
    private static Logger logger = Logger
            .getLogger(DefaultEndpointCallback.class);
    private IWorkflowConext oriContext;
    private IEndpointData epdata;

    public DefaultEndpointCallback(IWorkflowConext context, IEndpointData epdata) {
        this.oriContext = context;
        this.epdata = epdata;
    }

    public void onComplete(IWorkflowConext context, IEndpointData data) {
        // TODO Auto-generated method stub
        try {
            WorkflowExecuter.getInstance().executeCallBack(oriContext, epdata);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("callback ruleengine frame error, execute id ["
                    + oriContext.getContextProperty(
                            IWorkflowContextConstants.WORKFLOW_EXECUTE_ID)
                            .getValue() + "]", e);
        }
    }

    public void onError(Exception cause) {
        // TODO Auto-generated method stub
        try {
            oriContext.setExceptionCause(cause);
            WorkflowExecuter.getInstance().executeCallBack(oriContext, epdata);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("callback ruleengine frame error, execute id ["
                    + oriContext.getContextProperty(
                            IWorkflowContextConstants.WORKFLOW_EXECUTE_ID)
                            .getValue() + "]", e);
        }
    }

}
